using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Administration;

namespace TweakSP.Web.Admin
{
    public partial class TweakSP_ContentDatabases : GlobalAdminPageBase
    {
        protected Literal contentDbs;

        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder output = new StringBuilder();

            SPFarm myFarm = SPFarm.Local;
            SPServerCollection myServers = myFarm.Servers;
            foreach (SPServer myServer in myServers)
            {
                foreach (SPServiceInstance servinst in myServer.ServiceInstances) 
                {
                    if (servinst.Service is SPWebService) 
                    { 
                        SPWebService serv = (SPWebService)servinst.Service; 
                        foreach (SPWebApplication webapp in serv.WebApplications) 
                        {
                            output.Append("Web Application:&nbsp;&nbsp;" + webapp.GetResponseUri(SPUrlZone.Default).AbsoluteUri + "<br />");

                            foreach (SPContentDatabase cdb in webapp.ContentDatabases)
                            {
                                output.Append("&nbsp;&nbsp;Content DB:&nbsp;&nbsp;" + cdb.Name + "<br />");
                                output.Append("&nbsp;&nbsp;Size:&nbsp;&nbsp;" + DontShowMeBytes(cdb.DiskSizeRequired) + "<br />");
                                output.Append("&nbsp;&nbsp;Site Count:&nbsp;&nbsp;" + cdb.CurrentSiteCount.ToString() + "<br />");

                                foreach (SPSite site in cdb.Sites)
                                {
                                    output.Append("&nbsp;&nbsp;&nbsp;&nbsp;Site Collections:<br />");
                                    output.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Site:&nbsp;&nbsp;" + site.ServerRelativeUrl + "<br />");
                                    output.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Storage:&nbsp;&nbsp;" + DontShowMeBytes(site.Usage.Storage) + "<br />");
                                    output.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quota Limit:&nbsp;&nbsp;" + DontShowMeBytes(site.Quota.StorageMaximumLevel) + "<br />");
                                }

                                output.Append("<br /><br />");
                            }
                        } 
                    }
                } 
            }

            contentDbs.Text = output.ToString();

        }

        /// <summary>
        /// Mb sounds better
        /// </summary>
        private string DontShowMeBytes(ulong bytes)
        {
            decimal dBytes = bytes;
            dBytes = dBytes / 1024 / 1024;

            return dBytes.ToString("0.00") + " Mb";
        }

        private string DontShowMeBytes(long bytes)
        {
            return DontShowMeBytes((ulong)bytes);
        }

    }
}
